package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.datepicker.CalendarConstraints;
import com.google.android.material.datepicker.DateValidatorPointForward;
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.datepicker.MaterialDatePicker.Builder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import cn.lrwjz.androidguide.R;

public class DatePickerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_picker);

        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("DatePicker 示例");
        }

        // 基础日期选择器
        MaterialButton btnBasicDatePicker = findViewById(R.id.btnBasicDatePicker);
        btnBasicDatePicker.setOnClickListener(v -> {
            MaterialDatePicker<Long> datePicker = Builder.datePicker()
                    .setTitleText("选择日期")
                    .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
                    .build();

            datePicker.addOnPositiveButtonClickListener(selection -> {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                String formattedDate = sdf.format(selection);
                Toast.makeText(this, "选择的日期: " + formattedDate, Toast.LENGTH_SHORT).show();
            });

            datePicker.show(getSupportFragmentManager(), "DATE_PICKER");
        });

        // 日期范围选择器
        MaterialButton btnDateRangePicker = findViewById(R.id.btnDateRangePicker);
        btnDateRangePicker.setOnClickListener(v -> {
            MaterialDatePicker<androidx.core.util.Pair<Long, Long>> dateRangePicker =
                    MaterialDatePicker.Builder.dateRangePicker()
                            .setTitleText("选择日期范围")
                            .build();

            dateRangePicker.addOnPositiveButtonClickListener(selection -> {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                String startDate = sdf.format(selection.first);
                String endDate = sdf.format(selection.second);
                Toast.makeText(this, 
                        "选择的日期范围: " + startDate + " 至 " + endDate, 
                        Toast.LENGTH_SHORT).show();
            });

            dateRangePicker.show(getSupportFragmentManager(), "DATE_RANGE_PICKER");
        });

        // 带约束的日期选择器
        MaterialButton btnConstrainedDatePicker = findViewById(R.id.btnConstrainedDatePicker);
        btnConstrainedDatePicker.setOnClickListener(v -> {
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            calendar.add(Calendar.MONTH, 1); // 设置最大日期为一个月后

            CalendarConstraints constraints = new CalendarConstraints.Builder()
                    .setValidator(DateValidatorPointForward.now())
                    .setEnd(calendar.getTimeInMillis())
                    .build();

            MaterialDatePicker<Long> constrainedDatePicker = Builder.datePicker()
                    .setTitleText("选择日期（今日至一个月内）")
                    .setCalendarConstraints(constraints)
                    .setSelection(MaterialDatePicker.todayInUtcMilliseconds())
                    .build();

            constrainedDatePicker.addOnPositiveButtonClickListener(selection -> {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                String formattedDate = sdf.format(selection);
                Toast.makeText(this, "选择的日期: " + formattedDate, Toast.LENGTH_SHORT).show();
            });

            constrainedDatePicker.show(getSupportFragmentManager(), "CONSTRAINED_DATE_PICKER");
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 